local qiansong = fk.CreateSkill {
  name = "tyj_heg__qiansong",
}

Fk:loadTranslationTable {
  ["tyj_heg__qiansong"] = "谦颂",
  [":tyj_heg__qiansong"] = "当一名角色回满体力后，你可以令当前回合角色摸一张牌，然后你可以与其交换装备区内的一张牌。",
  ["#tyj_heg__qiansong-prompt"] = "是否对 %dest 发动 谦颂 令其摸一张牌？",
  ["#tyj_heg__qiansong-swap"] = "谦颂：是否与 %dest 交换装备区内的一张牌（选择其装备区内一张牌交换）？",
  ["$tyj_heg__qiansong1"] = "称美蜀政，祛其疑贰之心。",
  ["$tyj_heg__qiansong2"] = "蜀地君明民乐，实乃太平之治。",
}
Fk:addPoxiMethod {
  name = "#tyj_heg__qiansong-method",
  prompt = "谦颂：请选择你们装备区内的各一张牌来交换",
  card_filter = function(to_select, selected, data)
    if data and #selected < 2 then
      if table.contains(data[1][2], to_select) then
        local n = 0
        for _, id in ipairs(selected) do
          if table.contains(data[1][2], id) then
            n = n + 1
          end
        end
        if n < 1 then
          if #selected == 0 then return true end
          if #table.filter(data[1][2], function(cid)
                return Fk:getCardById(selected[1]).sub_type == Fk:getCardById(cid).sub_type
              end) > 0 then
            return Fk:getCardById(selected[1]).sub_type == Fk:getCardById(to_select).sub_type
          else
            return #table.filter(data[2][2], function(cid)
              return Fk:getCardById(to_select).sub_type == Fk:getCardById(cid).sub_type
            end) == 0
          end
        else
          return false
        end
      else
        local n = 0
        for _, id in ipairs(selected) do
          if table.contains(data[2][2], id) then
            n = n + 1
          end
        end
        if n < 1 then
          if #selected == 0 then return true end
          if #table.filter(data[2][2], function(cid)
                return Fk:getCardById(selected[1]).sub_type == Fk:getCardById(cid).sub_type
              end) > 0 then
            return Fk:getCardById(selected[1]).sub_type == Fk:getCardById(to_select).sub_type
          else
            return #table.filter(data[1][2], function(cid)
              return Fk:getCardById(to_select).sub_type == Fk:getCardById(cid).sub_type
            end) == 0
          end
        else
          return false
        end
      end
    end
  end,
  feasible = function(selected, data, extra_data)
    local can1 = false
    local can2 = false
    for _, id in ipairs(selected) do
      if table.contains(data[1][2], id) then
        can1 = true
      end
    end
    for _, id in ipairs(selected) do
      if table.contains(data[2][2], id) then
        can2 = true
      end
    end
    return can1 and can2 and #selected == 2
  end,
}
qiansong:addEffect(fk.HpRecover, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target:isWounded() and player.room.current and player.room.current:isAlive()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askToSkillInvoke(player, {
      skill_name = qiansong.name,
      prompt = "#tyj_heg__qiansong-prompt::" .. room.current.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if room.current and not room.current.dead then
      room.current:drawCards(1, self.name)
      if room.current and not room.current.dead and player and not player.dead and room.current ~= player and #player:getCardIds('e') > 0 and #room.current:getCardIds('e') > 0 then
        local cards1 = room.current:getCardIds("e")
        local cards2 = player:getCardIds("e")
        local cards = room:askToPoxi(player, {
          poxi_type = "#tyj_heg__qiansong-method",
          data = {
            { room.current.general, cards1 },
            { player.general,       cards2 }
          }
        })
        if #cards == 0 then return end
        local tcards = table.filter(cards, function(id) return table.contains(cards1, id) end)
        local pcards = table.filter(cards, function(id) return table.contains(cards2, id) end)
        room:swapCards(player, { { player, pcards }, { room.current, tcards } }, qiansong.name, Card.PlayerEquip)
      end
    end
  end,
})
return qiansong
